package myservice.android.activities;

import java.util.Calendar;

import myservice.android.Global;
import myservice.android.R;
import myservice.android.database.Database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ReportsYearActivity extends Activity {
	
	private LinearLayout creditTimeSection;
	private TextView bookCount, brochureCount, ministryTime, creditTime, magazineCount, returnCallCount, bibleStudyCount;
	private Button previousMonthButton, nextMonthButton;	
	private TextView currentYearText;	
	
	private int fromMonth, fromYear, toMonth, toYear;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);                
        setContentView(R.layout.reports_year);
        
        Calendar today = Calendar.getInstance();
        fromYear = today.get(Calendar.YEAR);
    	toYear=fromYear;        
        if (Global.yearRange==0) { // January - December
        	fromMonth=0;
        	toMonth=11;
        }
        else { // September - August
        	fromMonth=8;
        	toMonth=7;
        	if (today.get(Calendar.MONTH)>7)
        		toYear++;
        	else
        		fromYear--;
        }
    	
    	creditTimeSection = (LinearLayout) findViewById(R.id.creditTimeSection);
    	bookCount = (TextView) findViewById(R.id.bookCount);    	
    	brochureCount = (TextView) findViewById(R.id.brochureCount);    	
    	ministryTime = (TextView) findViewById(R.id.fieldMinistryTime);    	
    	creditTime = (TextView) findViewById(R.id.creditTime);    	
    	magazineCount = (TextView) findViewById(R.id.magazineCount);    	
    	returnCallCount = (TextView) findViewById(R.id.returnCallCount);    	
    	bibleStudyCount = (TextView) findViewById(R.id.bibleStudyCount);    	
    	currentYearText = (TextView) findViewById(R.id.selectedYear);  
        previousMonthButton = (Button) findViewById(R.id.previousButton);
        nextMonthButton = (Button) findViewById(R.id.nextButton);    	
    	
		previousMonthButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				switchYear(-1);				
			}			
		});
		
		nextMonthButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				switchYear(1);				
			}			
		});	    			  		

	}
    
    @Override
    public void onResume() {
    	super.onResume();
    	updateUi();
    }
    
    private void updateUi() {
    	
		currentYearText.setText(Global.monthNamesShort[fromMonth]+" "+fromYear+" - "+Global.monthNamesShort[toMonth]+" "+toYear);
		
		int fromNumber = fromYear*12+fromMonth;
		int toNumber = toYear*12+toMonth;
		
		// count spare minutes from previous months
		Cursor cursor = Database.select("SELECT SUM(minutes) AS m FROM ministry WHERE (year*12+month)<"+fromNumber);
		cursor.moveToNext();
		int spareMinutes = cursor.getInt(cursor.getColumnIndex("m")) % 60;
		cursor.close();
		
		// count all numbers in the selected year
		cursor = Database.select("SELECT SUM(hours) AS h, SUM(minutes) AS m, SUM(chours) AS ch, SUM(cminutes) AS cm, SUM(magazines) AS mag, SUM(brochures) AS br, SUM(books) AS bk, "+
		                                "SUM(returncalls) AS rc FROM ministry WHERE (year*12+month)>="+fromNumber+" AND (year*12+month)<="+toNumber);
		cursor.moveToNext();	
		
		bookCount.setText(""+cursor.getInt(cursor.getColumnIndex("bk")));
		brochureCount.setText(""+cursor.getInt(cursor.getColumnIndex("br")));
		returnCallCount.setText(""+cursor.getInt(cursor.getColumnIndex("rc")));
		magazineCount.setText(""+cursor.getInt(cursor.getColumnIndex("mag")));			
		
		// calculate ministry time
		int ministryMinutes = cursor.getInt(cursor.getColumnIndex("h"))*60 + cursor.getInt(cursor.getColumnIndex("m")) + spareMinutes;
		ministryTime.setText(ministryMinutes/60+" "+Global.res.getString(R.string.label_hour_short));
		
		// calculate credit time
		int creditMinutes = cursor.getInt(cursor.getColumnIndex("ch"))*60 + cursor.getInt(cursor.getColumnIndex("cm"));
		if (creditMinutes==0 && Global.trackCreditTime==false)
			creditTimeSection.setVisibility(View.GONE);
		else {
			creditTimeSection.setVisibility(View.VISIBLE);
			creditTime.setText(creditMinutes/60+" "+Global.res.getString(R.string.label_hour_short)+" "+creditMinutes % 60+" "+Global.res.getString(R.string.label_minute_short));
		}
		
		cursor.close();
		
		// calculate individual Bible students (status=Bible student, and at least one call marked as Bible study was conducted in that year)
		cursor = Database.select("SELECT DISTINCT name FROM persons JOIN calls ON persons.personid=calls.personid WHERE status=3 AND persons.timestamp<>"+Global.TIMESTAMP_DELETED+
				                 " AND biblestudy=1 AND (year*12+month)>="+fromNumber+" AND (year*12+month)<="+toNumber);
		bibleStudyCount.setText(""+cursor.getCount());
		cursor.close();
    	
    }
    
	private void switchYear(int direction) {
		fromYear=fromYear+direction;
		toYear=toYear+direction;					
		updateUi();
		
	}    

}